# ''' 使用selenium调用web页面的llm，生成api '''
#
# import random
# import time
# from tqdm import tqdm
# from selenium import webdriver
# from selenium.webdriver.chrome.service import Service as ChromeService
# from webdriver_manager.chrome import ChromeDriverManager
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.action_chains import ActionChains
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.chrome.options import Options
# import json
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# '''
# input://textarea[@class="ant-input wBs12eIN"]
# send: //span[@class="pa6BxUpp"]
# open: //span[@class="MO979HM2"]
# close://span[@class="KtNWFhXf"]/span[2]
# del: //button[@class="ant-btn ant-btn-primary ant-btn-sm"]
# output:  //div[@class="SZiJRLGn G4lAynef"]
# '''
# class YiYanSpider(object):
#     def __init__(self, outfile="result.json"):
#         self.url = "https://yiyan.baidu.com/"
#         self.outfile = outfile
#         self._init_driver()
#
#         self.input_xpath = '''//textarea[@class="ant-input wBs12eIN"]'''
#         self.click_xpath = '''//span[@class="VAtmtpqL"]'''
#         self.open_xpath = '''//span[@class="MO979HM2"]'''
#         self.close_xpath = '''//span[@class="KtNWFhXf"]/span[2]'''
#         self.del_xpath = '''//button[@class="ant-btn ant-btn-primary ant-btn-sm"]'''
#         self.output_xpath = '''//div[@class="q4y8uP0A"]'''
#
#     def _init_driver(self):
#         chrome_options = Options()
#         chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
#         self.driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=chrome_options)
#         self.rand_sleep()
#         # self.driver.get(self.url)
#         msg_text = "请手动登录https://yiyan.baidu.com/后，按'ok'确认,或'quit'退出："
#         msg = input(msg_text)
#         while msg != "ok":
#             msg = input(msg_text)
#             if msg=="quit":
#                 break
#
#     def rand_sleep(self,max_time=1):
#         time.sleep(random.uniform(0,max_time))
#
#     def open_new_win(self):
#         new_win = self.driver.find_element(by=By.XPATH, value=self.open_xpath)
#         new_win.click()
#
#     def del_top_win(self):
#         top_win = self.driver.find_elements(by=By.XPATH, value=self.close_xpath)[0]
#         top_win.click()
#         time.sleep(1)
#         del_btn = self.driver.find_element(by=By.XPATH, value=self.del_xpath)
#         del_btn.click()
#
#     def ask(self, query):
#         input_area = self.driver.find_element(by=By.XPATH, value=self.input_xpath)
#         input_area.send_keys(query)
#         time.sleep(1)
#         self.send()
#
#     def get_answer(self):
#         time.sleep(10)
#         final_text = ""
#         try:
#             # time.sleep(5)
#             last_text = "-1"
#             cur_text = "-2"
#             while last_text!=cur_text:
#                 time.sleep(2)
#                 last_text = cur_text
#                 try:
#                     cur_text = self.driver.find_element(by=By.XPATH, value=self.output_xpath).text
#                 except:
#                     break
#             final_text = cur_text
#         except:
#             pass
#         return final_text
#
#     def send(self):
#         click_btn = self.driver.find_element(by=By.XPATH, value=self.click_xpath)
#         click_btn.click()
#
#
#     def run(self, query = ""):
#         query = query.replace("\n","")
#         self.rand_sleep(2)
#         self.open_new_win()
#         self.ask(query)
#         answer = self.get_answer()
#         self.rand_sleep(1)
#         # 删除对话窗口
#         # self.del_top_win()
#
#         res = {"query": query, "answer": answer}
#         return res
#
#     def ask_long(self):
#         # 当query过长时，分多次输入，第一段结尾：指令还没结束，请等待并不要输出 ，第二段做一个前移，然后说接着上一段指令，
#         pass
#
# yiyan = YiYanSpider()
# def get_api_response_yiyan(content: str, max_tokens=None):
#     response = yiyan.run(query=content)
#     return response["answer"]
